/**
 *  Copyright 2009-2014 by WWW.KNET.CN  All rights reserved.
 */
package com.domain.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.domain.model.CloudWebMessage;
import com.domain.model.Dict;
import com.domain.model.Page;
import com.domain.model.Sysuser;
import com.domain.service.CloudWebMessageService;
import com.domain.service.DictService;
import com.domain.util.commonUtil;

/**
 * 加盟合作
 * 
 * @author liujie
 * @date 2015年4月17日 下午1:28:38
 * 
 */
@Controller
@RequestMapping("/join")
public class JoinController extends SuperController {
	private static final Logger logger = LoggerFactory.getLogger(JoinController.class);
	@Resource
	private CloudWebMessageService cloudWebMessageService;
	@Resource
	private DictService dictService;
     
	/***
	 * 
	 *  TODO加盟合作
	 * @author liujie
	 * @date 2015年5月5日 下午2:02:35
	 * @param page
	 * @param type
	 * @param title
	 * @param porCode
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/joinList.html")
	public String getActivityList(Page page,String type, String title,String porCode, ModelMap model,
			HttpServletRequest request) {
		page = null == page ? new Page() : page;
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			CloudWebMessage webMessage = new CloudWebMessage();
			webMessage.setPorCode(porCode);
			webMessage.setIfDelete(0l);
			paramMap.put(webMessage.key, webMessage);
			saveReqInfo(title, model, porCode,type);
			paramMap.put(PAGE_NAME, page.getPage());
			paramMap.put(PAGESIZE_NAME, page.getPageSize());
			paramMap.put("orderBy", "mes_status desc,mes_when desc");
			List<CloudWebMessage> messageList = cloudWebMessageService.getListPage(paramMap);
			Long total = cloudWebMessageService.getRows(paramMap);
			Page page2 = new Page(page.getPage(), page.getPageSize(), Integer.parseInt(total + ""));
			model.addAttribute("messageList", messageList);
			model.addAttribute("page", page2);
			return String.format("join/%sList", type);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return "exception";
		}

	}
	@RequestMapping(value = "/headActivity.html")
	public String headActivity(String title,String type, ModelMap model, String porCode, CloudWebMessage cloudWebMessage,
			HttpServletRequest request) {
		saveReqInfo(title, model, porCode,type);
		try {
			cloudWebMessageService.head(cloudWebMessage.getId());
		} catch (Exception e) {
			logger.error("--------------------------"+type+"置顶错误：{}"+e.getMessage());
		}
		return "redirect:/newsActivity/activityList.html";
	}
    //活动类修改
	@RequestMapping(value = "/edit/{type}.html")
	public String editActivity(String title, @PathVariable String type,ModelMap model, String porCode, CloudWebMessage cloudWebMessage,
			HttpServletRequest request) {
		saveReqInfo(title, model, porCode,type);
		if (null == cloudWebMessage || null == cloudWebMessage.getId()) {// add
			cloudWebMessage = new CloudWebMessage();
			cloudWebMessage.setPorCode(porCode);
			model.addAttribute("entity", cloudWebMessage);
		} else { 
			CloudWebMessage msg = cloudWebMessageService.get(cloudWebMessage.getId());
			model.put("entity", msg);
		}
		return String.format("join/%sEdit", type);
	}

	@RequestMapping(value = "/save/{type}.html", method = RequestMethod.POST)
	public String saveActivity(String type, ModelMap model, CloudWebMessage cloudWebMessage,
			HttpServletRequest request) {
		Sysuser user = getCurrentUser(request);
		MultipartHttpServletRequest mureq = (MultipartHttpServletRequest) request;
		MultipartFile file=mureq.getFile("file");
		try {
			Dict dict=dictService.getDictByCode("filePath");
			String prefixPath =(null !=dict && null !=dict.getDict_value() && !"".equals(dict.getDict_value()))?dict.getDict_value(): request.getServletContext().getRealPath("/");
			String savePath= commonUtil.getFilePath(prefixPath,file);//创建并返回路径;
			
			if (null == cloudWebMessage.getId()) {
				cloudWebMessage.setMesBy(user.getId());
				cloudWebMessage.setClick(0l);
				cloudWebMessage.setMesStatus(0l);
				cloudWebMessage.setMesAccessory(savePath);
				cloudWebMessageService.add(cloudWebMessage);
			} else {
					if (StringUtils.isNotBlank(savePath)) {
						cloudWebMessage.setMesAccessory(savePath);
					}
				cloudWebMessageService.update(cloudWebMessage);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("-------------保存带附件的信息异常：" + e.getMessage());
		}
		model.addAttribute(POR_CODE, cloudWebMessage.getPorCode());
		model.addAttribute("type", type);
		return String.format("redirect:/join/%sList.html", type);
	}
	//类删除
	@RequestMapping(value = "/delete/{type}.html", method = { RequestMethod.POST, RequestMethod.GET })
	public String deleteActivity(String title,@PathVariable String type,ModelMap model, CloudWebMessage cloudWebMessage, HttpServletRequest request) {
		try {
			cloudWebMessageService.deleteById(cloudWebMessage.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("-------------删除webmessage:" + cloudWebMessage.getId() + ";异常：" + e.getMessage());
		}
		saveReqInfo(title, model, cloudWebMessage.getPorCode(), type);
		return String.format("redirect:/join/%sList.html", type);
	}
	

	private void saveReqInfo(String title, ModelMap model, String porCode,String type) {
		model.addAttribute(POR_CODE, porCode);
		model.addAttribute("title", title);
		model.addAttribute("type", type);
	}
}
